import { createApp } from 'vue'
import './style.css'
import App from './App.vue'
import router from './router'
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
import ElementPlus from 'element-plus'
import store from './store'
const app = createApp(App);

import PanelHead from './components/panelHead.vue'
import zhCn from 'element-plus/dist/locale/zh-cn.mjs'

// 路由守卫(处理非鉴权的请求)
router.beforeEach((to, from )=> {
    const token = localStorage.getItem('pz_token');
    // token 不存在，跳转回登陆页面
    if(!token && to.path !=='/login'){
        return '/login';
    } else if (token && to.path == '/login'){
        // 当token存在，用户想通过浏览器转login时，停留在首页
        return '/';
    } else {
        // 其他情况，正常返回
        return true
    }

})

// 图标全局注册
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
    app.component(key, component)
}

app.component('PanelHead',PanelHead)
app.use(ElementPlus,{
    locale: zhCn,
})
// 路由挂载
app.use(router);
app.use(store);
app.mount('#app');
